//
//  OrderTableViewCell.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "OrderTableViewCell.h"

@interface OrderTableViewCell ()

@property (nonatomic, strong) UIView * backView;

@property (nonatomic, strong) UIView * timeView;
@property (nonatomic, strong) UIImageView * timeImageView;
@property (nonatomic, strong) UILabel * timeLabel;
@property (nonatomic, strong) UILabel * priceLabel;

@property (nonatomic, strong) UIView *  originView;
@property (nonatomic, strong) UIImageView * originImageView;
@property (nonatomic, strong) UILabel * originLabel;


@property (nonatomic, strong) UIView *  destinationView;
@property (nonatomic, strong) UIImageView * destinationImageView;
@property (nonatomic, strong) UILabel * destinationLabel;


@property (nonatomic, strong) UIView * personCountView;
@property (nonatomic, strong) UIImageView * personCountImageView;
@property (nonatomic, strong) UILabel * personCountLabel;
@property (nonatomic, strong) UILabel * detailLabel;

@property (nonatomic, strong) UILabel *spreadFeeLabel;

@end

@implementation OrderTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatMainView];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = BACKCOLOR;
    }
    return self;
}


-(void)setOrderValueWith:(OrderModel *)model{
    
    self.timeLabel.text = model.departureTime;
    self.priceLabel.text = [NSString stringWithFormat:@"交易金额: %.2f",model.price];
    
    self.originLabel.text = model.boardingPoint;
    self.destinationLabel.text = model.breakoutPoint;
    
    self.personCountLabel.text = [NSString stringWithFormat:@"%ld人同行",model.Seats];
    
    
    NSMutableAttributedString * spreadStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"推广费用:%.2f元",model.cash] attributes:nil];
    [spreadStr setAttributes:@{NSForegroundColorAttributeName:BLACKCOLOR} range:NSMakeRange(0, 5)];
    [spreadStr setAttributes:@{NSForegroundColorAttributeName:ORANGCOLOR} range:NSMakeRange(5, spreadStr.length - 5)];
    self.spreadFeeLabel.attributedText = spreadStr;
    
    NSMutableAttributedString * spreadRateStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"推广比例:%.0f%%",model.promotionRatio * 100] attributes:nil];
    [spreadRateStr setAttributes:@{NSForegroundColorAttributeName:BLACKCOLOR} range:NSMakeRange(0, 5)];
    [spreadRateStr setAttributes:@{NSForegroundColorAttributeName:ORANGCOLOR} range:NSMakeRange(5, spreadRateStr.length - 5)];
    self.detailLabel.attributedText = spreadRateStr;
//    self.detailLabel.text = [NSString stringWithFormat:@"推广比例:%.0f%%",model.promotionRatio * 100];
    
}

-(void)creatMainView{
    
    [self.contentView addSubview:self.backView];
    
    [self.backView addSubview:self.timeView];
    [self.timeView addSubview:self.timeImageView];
    [self.timeView addSubview:self.timeLabel];
    [self.timeView addSubview:self.priceLabel];
    
    [self.backView addSubview:self.originView];
    [self.originView addSubview:self.originImageView];
    [self.originView addSubview:self.originLabel];
    
    
    [self.backView addSubview:self.destinationView];
    [self.destinationView addSubview:self.destinationImageView];
    [self.destinationView addSubview:self.destinationLabel];
    
    
    [self.backView addSubview:self.personCountView];
    [self.personCountView addSubview:self.personCountImageView];
    [self.personCountView addSubview:self.personCountLabel];
    [self.personCountView addSubview:self.spreadFeeLabel];
    [self.personCountView addSubview:self.detailLabel];
    
    [self updateConstraints];
}

-(void)updateConstraints{
    [super updateConstraints];
    
    [self.backView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView).offset(0);
        make.right.equalTo(self.contentView).offset(0);
        make.bottom.equalTo(self.contentView).offset(-10);
        
    }];
    
    
    [_timeView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.backView).offset(5);
        make.left.equalTo(self.backView);
        make.right.equalTo(self.backView);
        make.height.equalTo(@30);
        
        
    }];
    
    [_timeImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_timeView).offset(11);
        make.left.equalTo(_timeView).offset(10);
        make.size.mas_offset(CGSizeMake(8, 8));
        
    }];
    
    
    [_timeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_timeView);
        make.left.equalTo(_timeImageView.mas_right).offset(5);
        make.bottom.equalTo(_timeView);
        make.width.equalTo(@145);
    }];
    
    
    [_priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_timeView);
        make.bottom.equalTo(_timeView);
        make.right.equalTo(_timeView).offset(-5);
        make.left.equalTo(_timeLabel.mas_right).offset(10);
        
    }];
    
    
    [_originView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.timeView.mas_bottom);
        make.left.equalTo(self.backView);
        make.right.equalTo(self.backView);
        make.height.equalTo(@30);
        
        
    }];
    
    [_originImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_originView);
        make.left.equalTo(_timeImageView);
        make.size.equalTo(_timeImageView);
        
    }];
    
    
    [_originLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_originView);
        make.left.equalTo(_timeLabel);
        make.bottom.equalTo(_originView);
        make.right.equalTo(_originView).offset(-10);
        
    }];
    
    
    
    
    [_destinationView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.originView.mas_bottom);
        make.left.equalTo(self.backView);
        make.right.equalTo(self.backView);
        make.height.equalTo(@30);
        
        
    }];
    
    [_destinationImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_destinationView);
        make.left.equalTo(_timeImageView);
        make.size.equalTo(_timeImageView);
        
    }];
    
    [_destinationLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_destinationView);
        make.left.equalTo(_timeLabel);
        make.bottom.equalTo(_destinationView);
        make.right.equalTo(_destinationView).offset(-10);
    }];
    
    
    [_personCountView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.destinationView.mas_bottom);
        make.left.equalTo(self.backView);
        make.right.equalTo(self.backView);
        make.height.equalTo(@30);
        
    }];
    
    [_personCountImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(_personCountView);
        make.left.equalTo(_timeImageView);
        make.size.equalTo(_timeImageView);
        
    }];
    
    
    [_personCountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_personCountView);
        make.left.equalTo(_timeLabel);
        make.bottom.equalTo(_personCountView);
        make.width.equalTo(@50);
        
    }];
    
    [self.spreadFeeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.personCountView);
        make.right.equalTo(self.priceLabel).with.offset(-10);
    }];
    
    [_detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(self.personCountView);
        make.right.equalTo(self.spreadFeeLabel.mas_left).with.offset(-10);
//        make.left.equalTo(self.personCountLabel.mas_right).offset(5);
        
    }];
    
}


-(UIView *)backView{
    if (!_backView) {
        _backView = [[UIView alloc]init];
        _backView.backgroundColor = [UIColor whiteColor];
    }
    return _backView;
}


-(UIView *)timeView{
    
    if (!_timeView) {
        _timeView = [[UIView alloc]init];
        _timeView.backgroundColor = [UIColor whiteColor];
    }
    
    return _timeView;
    
}

-(UIImageView *)timeImageView{
    
    if (!_timeImageView) {
        _timeImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"order_time"]];
    }
    return _timeImageView;
    
}

-(UILabel *)timeLabel {
    
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.textColor = BLACKCOLOR;
        _timeLabel.font = [UIFont systemFontOfSize:14];
//        _timeLabel.backgroundColor = BACKCOLOR;
    }
    return _timeLabel;
    
}

-(UILabel *)priceLabel {
    
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc]init];
        _priceLabel.textColor = GRAYCOLOR;
        _priceLabel.font = [UIFont systemFontOfSize:14];
        _priceLabel.textAlignment = NSTextAlignmentRight;
//        _priceLabel.backgroundColor = BACKCOLOR;
    }
    return _priceLabel;
    
}


-(UIView *)originView{
    if (!_originView) {
        _originView = [[UIView alloc]init];
        _originView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _originView;
}

-(UIImageView *)originImageView{
    
    if (!_originImageView) {
        _originImageView = [[UIImageView alloc]init];
        _originImageView.image = [UIImage imageNamed:@"order_origin"];
        //        _destinationImageView.backgroundColor = GRAYCOLOR;
    }
    
    return _originImageView;
}

-(UILabel *)originLabel{
    if (!_originLabel) {
        _originLabel = [[UILabel alloc]init];
        _originLabel.textColor = BLACKCOLOR;
        _originLabel.font = [UIFont systemFontOfSize:14];
        //        _originLabel.text = @"郑州 建正东方中心";
    }
    return _originLabel;
}

-(UIView *)destinationView{
    if (!_destinationView) {
        _destinationView = [[UIView alloc]init];
        _destinationView.backgroundColor = [UIColor whiteColor];
        
    }
    
    return _destinationView;
}

-(UIImageView *)destinationImageView{
    
    if (!_destinationImageView) {
        _destinationImageView = [[UIImageView alloc]init];
        _destinationImageView.image = [UIImage imageNamed:@"order_destination"];
        //        _destinationImageView.backgroundColor = GRAYCOLOR;
    }
    
    return _destinationImageView;
}

-(UILabel *)destinationLabel{
    if (!_destinationLabel) {
        _destinationLabel = [[UILabel alloc]init];
        _destinationLabel.textColor = BLACKCOLOR;
        _destinationLabel.font = [UIFont systemFontOfSize:14];
        //        _destinationLabel.text = @"洛阳 河南科技大学周山校区 ";
    }
    return _destinationLabel;
}


-(UIView *)personCountView {
    
    if (!_personCountView) {
        _personCountView = [[UIView alloc]init];
        _personCountView.backgroundColor = [UIColor whiteColor];
        
    }
    return _personCountView;
}

-(UIImageView *)personCountImageView {
    
    if (!_personCountImageView) {
        _personCountImageView = [[UIImageView alloc]init];
        _personCountImageView.image = [UIImage imageNamed:@"order_personcount"];
        //        _remarkImageView.backgroundColor = GRAYCOLOR;
    }
    return _personCountImageView;
}

-(UILabel *)personCountLabel{
    
    if (!_personCountLabel) {
        _personCountLabel = [[UILabel alloc]init];
        _personCountLabel.textColor = BLACKCOLOR;
        _personCountLabel.textAlignment = NSTextAlignmentLeft;
        _personCountLabel.font = [UIFont systemFontOfSize:12];

    }
    
    return _personCountLabel;
}

-(UILabel *)detailLabel{
    
    if (!_detailLabel) {
        _detailLabel = [[UILabel alloc]init];
        _detailLabel.textColor = BLACKCOLOR;
        _detailLabel.textAlignment = NSTextAlignmentRight;
        _detailLabel.font = [UIFont systemFontOfSize:12];
//        _detailLabel.backgroundColor = BACKCOLOR;

    }
    
    return _detailLabel;
}

- (UILabel *)spreadFeeLabel {
    if (!_spreadFeeLabel) {
        _spreadFeeLabel = [[UILabel alloc] init];
        _spreadFeeLabel.textColor = BLACKCOLOR;
        _spreadFeeLabel.textAlignment = NSTextAlignmentRight;
        _spreadFeeLabel.font = [UIFont systemFontOfSize:12];
    }
    return _spreadFeeLabel;
}

@end
